<template>
	<view class="content">
		<view class="closeWrap">
			<image @tap="comeback" class="closeImg" src="../../static/img/icon_return.png"></image>
		</view>
		<view class="lockWrap">
			<image class="lock" src="../../static/img/reset/lock-3@2x.png"></image>
		</view>
		<view class="lockWrap">
			<text class="lockContent" v-if="operateType=='FORGET'">找回密码</text>
			<text class="lockContent" v-if="operateType=='MODIFY'">修改密码</text>
		</view>
		<view class="loginWrap">
			<view class="login" v-show="operateType=='FORGET'">
				<input class="inputs"  v-model="phone" placeholder="输入手机号" maxlength="11"
					placeholder-class="inputPlaceholder" />
			</view>
			<view class="login">
				<input class="inputs" v-model="smsCode" placeholder="输入验证码" placeholder-class="inputPlaceholder" />
				<text v-if="!isCode" @tap="getCode" class="eyeCode">获取验证码</text>
				<view v-else class="eyeCodes">
					<image @tap="smsCode=''" class="icon_close" src="../../static/img/icon_close.png"></image>
					<text style="color: #BFBFBF;font-size: 28rpx;">{{secondsLeft}}秒后重发</text>
				</view>
			</view>
			<view class="login">
				<input class="inputs" :password="isEye" v-model="newPwd" placeholder="请输入新密码"
					placeholder-class="inputPlaceholder" />
				<image v-if="isEye" @tap="isEye=!isEye" class="eye" src="../../static/img/eye-off.png"></image>
				<image v-else @tap="isEye=!isEye" class="eye" src="../../static/img/eye.png"></image>
			</view>
			<text class="prompt">请输入最少8位含数字、大写字母和小写字母的密码</text>
		</view>
		<view class="agreementWrap" v-show="false">
			<radio :checked="isAgree" style="transform: scale(0.7);" @tap="isAgree = !isAgree"></radio>
			<text class="agree">阅读即同意</text>
			<text v-for="(item,index ) in agreementList" :key="index" style="display: flex;align-items: center;flex-wrap: wrap;">
				<text style="color: #6B64D3;font-size: 24rpx;"><text v-if="index==agreementList.length-1"
						class="agree">和</text>《{{item.agreementTitle}}》<text v-if="index<agreementList.length-2"
						class="agree">、</text></text>
			</text>
		</view>
		<view class="submitWrap">
			<view @tap="doRestPwd" class="submit" style="background: #6B64D3;">
				<text style="font-size: 32rpx;color: #F5F5F5;letter-spacing: 2rpx;">确定</text>
			</view>
		</view>
		
		<view v-if="isPop" class="popUp">
			<view class="pop">
				<view class="popTitle">
					<text style="font-weight: 700;font-size: 34rpx;color: #262626;">隐私保护说明</text>
				</view>
				<view style="width: 81%;margin-left: 9.5%;">
					<text>
						<text class="popTxt">为保障您的个人权益，登录前请先阅读</text>
						<text v-for="(item,index) in agreementList">
							<text class="agreeTxt">《{{item.agreementTitle}}》</text>
							<text class="popTxt" v-if="index<agreementList.length-1">、</text>
						</text>
						<text class="popTxt"> 同意并勾选后再登录。</text>
					</text>
				</view>
				<view class="toAgreeWrap">
					<view @tap="isPop=false;isAgree=true;" class="toAgrees">
						<text style="font-size: 32rpx;color: #fff;">同意并继续</text>
					</view>
					<view @tap="isPop=false" class="toAgreeNo">
						<text style="font-size: 32rpx;color: #595959;">不同意并退出</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { ref } from 'vue'
	import { forgetPassword, sendSmsCode,queryAgreement } from '@/utils/api/index';
	import { onShow, onLoad, } from '@dcloudio/uni-app'
	const phone = ref('') //手机号
	const smsCode = ref('')
	const smsId = ref('')
	const isEye = ref(true)
	const comeback = () => {
		uni.navigateBack()
	}
	const timer = ref()
	const isCode = ref(false)
	const secondsLeft = ref(60)
	const newPwd = ref('') //新密码
	const agreementList = ref([])
	const isAgree =ref(false)
	const isPop = ref(false)
	const operateType=ref('FORGET')
	const getCode = async () => {
		if (phone.value=='') {
			uni.showToast({
				title: '请填写手机号',
				icon: 'error'
			});
			return
		}
		const res = await sendSmsCode({ data: { phone: phone.value } })
		uni.showToast({
			title: '短信发送成功',
			icon: 'success'
		});
		smsId.value = res.data
		isCode.value = true
		secondsLeft.value = 60
		timer.value = setInterval(() => {
			if (secondsLeft.value > 0) {
				secondsLeft.value--;
			} else {
				clearInterval(timer.value!);
				isCode.value = false
				secondsLeft.value = 60
			}
		}, 1000);
	}

	const doRestPwd = async () => {
		// if (!isAgree.value) {
		// 	isPop.value = true
		// 	return
		// }	
		const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;
		if(!regex.test(newPwd.value)){
			uni.showToast({
				title: '密码太简单请重新输入',
				icon: 'error'
			});
			return
		}
		
		if (phone.value=='') {
			uni.showToast({
				title: '请填写手机号',
				icon: 'error'
			});
			return
		}
		if (smsCode.value=='') {
			uni.showToast({
				title: '请输入验证码',
				icon: 'error'
			});
			return
		}
		if (smsId.value=='') {
			uni.showToast({
				title: '请先获取验证码',
				icon: 'error'
			});
			return
		}
		const pwdData={
			smsCode:smsCode.value,
			smsId:smsId.value,
			phone:phone.value,
			newPwd:newPwd.value,
			confirmPwd:newPwd.value
		}
		await forgetPassword({data:pwdData})
		
		if(operateType.value=='FORGET'){
			uni.showToast({
				title: '重置密码成功',
				icon: 'success'
			});
		}
		if(operateType.value=='MODIFY'){
			uni.showToast({
				title: '修改密码成功',
				icon: 'success'
			});
		}
		
		setTimeout(()=>{
			comeback()
		},500)
		
		
	}
	
	const loadAgreement = async () => {
		const res = await queryAgreement({
			data: {
				agreementType: 'LOGIN'
			}
		})
		agreementList.value = res.data
	}
	loadAgreement()
	
	
	onLoad((option)=>{
		const type=option.operateType
		if(type){
			operateType.value=type
		}
		phone.value=option.phone
	})
</script>

<style scoped>
	.lockWrap {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.lock {
		width: 168rpx;
		height: 168rpx;
		margin-top: 12%;
	}
	
	.lockContent {
		font-size: 48rpx;
		color: #262626;
		width: 192rpx;
		height: 67rpx;
		font-weight: bold;
	}
	.content {
		width: 100%;
		padding-top: var(--status-bar-height);
		background: url('../../static/img/bj.png') no-repeat;
	}

	.bj {
		width: 100%;
		height: 1624rpx;
		position: absolute;
	}

	.closeWrap {
		width: 100%;
		height: 150rpx;
		display: flex;
		align-items: center;
	}

	.closeImg {
		width: 36rpx;
		height: 36rpx;
		margin-left: 35rpx;
	}

	.title {
		margin-top: 15%;
		margin-left: 10%;
	}

	.tip {
		margin-top: 15rpx;
		margin-left: 10%;
	}

	.loginWrap {
		width: 80%;
		height: 220rpx;
		margin-top: 40rpx;
		margin-left: 10%;
	}

	.login {
		width: 100%;
		height: 110rpx;
		border-bottom: 1rpx solid #EFEEF1;
		display: flex;
		position: relative;
	}

	.inputPlaceholder {
		color: #CED4DB;
		font-size: 32rpx;
	}

	.inputs {
		width: 100%;
		height: 110rpx;
		box-sizing: border-box;
	}

	.submitWrap {
		width: 100%;
		margin-top: 140rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.submit {
		width: 638rpx;
		height: 88rpx;
		background: #F0F0F0;
		border-radius: 22rpx 22rpx 22rpx 22rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.eye {
		width: 36rpx;
		height: 36rpx;
		position: absolute;
		right: 0;
		top: 40rpx;
	}

	.eyeCode {
		height: 36rpx;
		color: #6B64D3;
		position: absolute;
		right: 0;
		top: 40rpx;
	}

	.eyeCodes {
		width: 220rpx;
		height: 80rpx;
		position: absolute;
		right: 0;
		top: 40rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.icon_close {
		width: 32rpx;
		height: 32rpx;
		margin-top: 2rpx;
	}
	
	.agreementWrap {
		width: 80%;
		margin-top: 200rpx;
		margin-left: 10%;
		display: flex;
		align-items: center;
		flex-wrap: wrap;
	}
	
	.agree {
		color: #8C8C8C;
		font-size: 24rpx;
	}
	
	.popUp {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.pop {
		width: 600rpx;
		height: 580rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		position: relative;
	}
	
	.popTitle {
		width: 100%;
		height: 144rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.popTxt {
		font-size: 28rpx;
		color: #595959;
		letter-spacing: 2rpx;
	}
	
	.agreeTxt {
		font-size: 28rpx;
		color: #6B64D3;
		letter-spacing: 2rpx;
	}
	
	.toAgreeWrap {
		width: 100%;
		margin-top: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.toAgrees {
		width: 488rpx;
		height: 88rpx;
		background: #6B64D3;
		border-radius: 22rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.toAgreeNo {
		width: 488rpx;
		height: 120rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.inputPop {
		width: 100%;
		height: 80rpx;
		background: #f4f6fa;
		border-radius: 24rpx;
		padding-left: 20rpx;
	}
	.prompt{
		color: #ccc;
		font-size: 24rpx;
	}
</style>